package com.bytecodezz.app.base.toolset.effect

import android.graphics.Canvas
import android.graphics.Paint
import android.text.style.ReplacementSpan

/**
 * @author: bytecodezz
 * @version: 1.0
 * @time: 2021/7/14 14:57
 * @package: com.bytecodezz.app.base.toolset.effect
 *
 * @description:为指定位置内的Text文字添加mMargin长度的间距
 * 使用方式：
 * val spannableBuilder = SpannableStringBuilder("String all")
 * 为第二个字符添加 15的后间距：
 * spannableBuilder.setSpan(SpanAppendSpace(15),2,3,Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
 * textView.text = spannableBuilder
 */
class SpanAppendSpace constructor(val mMargin: Int = 0) : ReplacementSpan() {
    override fun getSize(
        paint: Paint,
        text: CharSequence?,
        start: Int,
        end: Int,
        fm: Paint.FontMetricsInt?
    ): Int {
        return (paint.measureText(text, start, end) + mMargin).toInt()
    }

    override fun draw(
        canvas: Canvas,
        text: CharSequence?,
        start: Int,
        end: Int,
        x: Float,
        top: Int,
        y: Int,
        bottom: Int,
        paint: Paint
    ) {
        //绘制文字
        if (text != null) {
            paint.isAntiAlias = true;
            canvas.drawText(text, start, end, x, y.toFloat(), paint)
        }
    }
}